package nadav.tasher.tool1.slice;


import nadav.tasher.lightool.graphics.view.ExpandingView;
import nadav.tasher.lightool.graphics.view.appview.AppView;
import nadav.tasher.lightool.graphics.view.appview.corner.Corner;
import nadav.tasher.tool1.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.Point;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    AppView view;
    static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "chenyu");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        view = new AppView(this);
        view.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
        view.getDrawer().getDrawerView().setBackground(getCoaster(Color.BLACK.getValue(), 64, 20));
        view.getDrawer().setAnimationTime(1000);
        Corner c = new Corner(this, screenX(this) / 5, 0xFF123987);
        Image imageView = new Image(getApplicationContext());
        imageView.setPixelMap(ResourceTable.Media_icon);
        c.setView(imageView, 0.8);
        c.addOnState(new Corner.OnState() {
                         @Override
                         public void onOpen() {
                         }

                         @Override
                         public void onClose() {
                         }

                         @Override
                         public void onBoth(boolean isOpened) {
                             if (view.getDrawer().isOpen()) {
                                 view.getDrawer().close();
                             } else {
                                 view.getDrawer().open(0.5);
                             }
                         }
                     }
        );
        view.getCornerView().setTopRight(c);
        Corner c2 = new Corner(this, screenX(this) / 5, 0xFF123987);
        c2.addOnState(new Corner.OnState() {
                          @Override
                          public void onOpen() {
                          }

                          @Override
                          public void onClose() {
                          }

                          @Override
                          public void onBoth(boolean isOpened) {
                              view.getDrawer().open(0.8);
                          }
                      }
        );
        view.getCornerView().setBottomLeft(c2);
        view.setBackgroundColor(Color.GRAY.getValue());

        Text myTextA=new Text(this);
        myTextA.setLayoutConfig(new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, (int) (screenY(getApplicationContext())/7)));
        myTextA.setText("This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.");

        myTextA.setText("This is a Text");
        myTextA.setTextAlignment(TextAlignment.CENTER);
        Text mTextB=new Text(getApplicationContext());
        mTextB.setTextAlignment(TextAlignment.CENTER);
        mTextB.setTextSize(32, Text.TextSizeType.FP);
        mTextB.setText("This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.This Is A Text.\nThis Is A Text.");
        mTextB.setText("ThisISMOVN");
        ShapeElement shapeElement=new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        mTextB.setBackground(shapeElement);
        mTextB.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT));
//        mTextB.estimateSize(ComponentContainer.LayoutConfig.MATCH_PARENT, Component.EstimateSpec.getSizeWithMode(0, Component.EstimateSpec.UNCONSTRAINT));
//        //mTextB.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
//        mTextB.setHeight(60);
//        HiLog.info(TAG,"mTextB.getHeight"+mTextB.getHeight());
//        HiLog.info(TAG, "mTextB.getEstimatedHeight = " +mTextB.getEstimatedHeight());
//        HiLog.info(TAG, "mTextA.getEstimatedHeight = " +myTextA.getEstimatedHeight());

        ExpandingView ev=new ExpandingView(this);
       // ev.setBackground(getCoaster(Color.WHITE.getValue(),32,32));
        ev.setBackground(getCoaster(Color.WHITE.getValue(),32,10));
        ev.setDuration(3000);
        ev.setPadding(0,0);
        ev.setTop(myTextA);
        ev.setBottom(mTextB);
        DirectionalLayout ll=new DirectionalLayout(this);
        ll.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        ll.setAlignment(LayoutAlignment.CENTER);
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.addComponent(ev);
        view.getScrolly().setView(ll);
        setUIContent(view);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public Element getCoaster(int color, int radii, int padding) {
        ShapeElement coaster = new ShapeElement();
        coaster.setRgbColor(RgbColor.fromArgbInt(color));
        coaster.setCornerRadius(radii);
        coaster.setBounds(padding, padding, padding+coaster.getWidth(), padding+coaster.getHeight());
        return coaster;
    }

    public  int screenY(Context con) {
        Point p = new Point();
        DisplayManager.getInstance().getDefaultDisplay(this.getContext()).get().getSize(p);
        return (int)p.getPointY();
    }

    public  int screenX(Context con) {
        Point p = new Point();
        DisplayManager.getInstance().getDefaultDisplay(this.getContext()).get().getSize(p);
        return (int)p.getPointX();
    }
}

